<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>


<script type="text/javascript" src="js/jquery.itextclear.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea', '.form').iTextClear();
    });
</script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="${ctx}/css/Tree.css"/>
<!-- jQplot CSS END -->

<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen" href="lib/datatables/css/vpad.css"/>
<script type="text/javascript" src="lib/datatables/js/jquery.dataTables.js"></script>
<!-- DATATABLES CSS END -->

<h1 class="page-title">职务代理人设置 - 添加</h1>

<div class="container_12 clearfix leading">

    <%@ include file="/WEB-INF/pages/common/common_tree.jsp" %>

    <div class="side grid_8 leading">
        <form class="form has-validation"  name="post_form" id="post_form" method="post">
            <div class="clear"></div>
            <div class="clearfix">
                <label for="duty_sel" class="form-label">职务 <em>*</em></label>

                <div class="form-input"><select name="proxy.duty.id" id="duty_sel"> </select></div>
            </div>

            <div class="clearfix">
                <label for="employee_sel" class="form-label">任职人 <em>*</em></label>

                <div class="form-input"><select name="proxy.employee.id" id="employee_sel"></select></div>
            </div>

            <div class="clearfix">
                <label for="proxy_one_sel" class="form-label">第一代理人 <em>*</em></label>

                <div class="form-input"><select name="proxy.proxyOne.id" id="proxy_one_sel"></select></div>
            </div>

            <div class="clearfix">
                <label for="proxy_two_sel" class="form-label">第二代理人 <em>*</em></label>

                <div class="form-input"><select name="proxy.proxyTwo.id" id="proxy_two_sel"></select></div>
            </div>

            <div class="clearfix">
                <label for="proxy_three_sel" class="form-label">第三代理人 <em>*</em></label>

                <div class="form-input"><select name="proxy.proxyThree.id" id="proxy_three_sel"></select></div>
            </div>
            <div class="clearfix">
                <div class="clearfix">
                    <div class="form-input"></div>
                </div>
            </div>
            <div class="form-action clearfix">
                <input type="hidden" name="proxy.id" value="${proxy.id}" />
                <button class="button" type="button" onclick="checkForm()"><s:if test="#request.proxy == null">添加</s:if><s:else>修改</s:else></button>
                <button class="button" type="reset">重置</button>
                <a class="button" href="#duty_proxy/main.xhtml?operation=set">
                    <span style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>
            </div>
        </form>
    </div>
</div>

<div class="clear"></div>


<script type="text/javascript" src="js/Tree.js"></script>
<script type="text/javascript">
    $(function() {
        findEmployeeForDutyProxy(0);
    });

    function findDuty(organizationId) {
        if("${proxy.duty.id}" > 0){
            $("#uniform-duty_sel span:first").text("${proxy.duty.dutyName}");//初始化
            $("#duty_sel").attr("disabled", true);
            return;
        }

        $.getJSON("${ctx}/ajax/duty-list.xhtml?organizationId=" + organizationId, function(duties) {
            $("#uniform-duty_sel span:first").text("");//清除上次选择的职务
            var selObj = $("#duty_sel");
            selObj.html("");
            if (duties != null && duties.length != 0) {
                $.each(duties, function(i, duty) {
                    if(i == 0){
                        $("#uniform-duty_sel span:first").html(duty.dutyName);//设置本次选择的职务
                    }
                    selObj.append("<option value='" + duty.id + "'>" + duty.dutyName + "</option>")
                });
                //selObj.get(0).selectedIndex = 1;
            }

            //init
            if("${proxy.duty.id}" > 0){
                $("#uniform-duty_sel span:first").text("${proxy.duty.dutyName}");//初始化
            }
        });
    }
    //查找指定类型的雇员(type: 0为任职人， 1为第一代理 人，2为第二代理人，3为第三代理人)
    function findEmployeeForDutyProxy(type) {
        $.getJSON("${ctx}/ajax/proxy_employee.xhtml?type=" + type, function(employees) {
            if (employees != null && employees.length > 0) {
                var employeeObj = $("#employee_sel");
                var proxyOneObj = $("#proxy_one_sel");
                var proxyTwoObj = $("#proxy_two_sel");
                var proxyThreeObj = $("#proxy_three_sel");
                temp = "<option value='0'>请选择</option>";
                employeeObj.append(temp);
                proxyOneObj.append(temp);
                proxyTwoObj.append(temp);
                proxyThreeObj.append(temp);
                $.each(employees, function(i, employee) {
                    temp = "<option value='" + employee.id + "'>" + employee.name + "</option>";
                    employeeObj.append(temp);
                    proxyOneObj.append(temp);
                    proxyTwoObj.append(temp);
                    proxyThreeObj.append(temp);
                });
            }

            //init
            if("${proxy.employee.id}" > 0){
                $("#uniform-employee_sel span:first").text("${proxy.employee.name}");//初始化
                $("#employee_sel").val("${proxy.employee.id}");
            }
            if("${proxy.proxyOne.id}" > 0){
                $("#uniform-proxy_one_sel span:first").text("${proxy.proxyOne.name}");//初始化
                $("#proxy_one_sel").val("${proxy.proxyOne.id}");
            }
            if("${proxy.proxyTwo.id}" > 0){
                $("#uniform-proxy_two_sel span:first").text("${proxy.proxyTwo.name}");//初始化
                $("#proxy_two_sel").val("${proxy.proxyTwo.id}");
            }
            if("${proxy.proxyTwo.id}" > 0){
                $("#uniform-proxy_three_sel span:first").text("${proxy.proxyThree.name}");//初始化
                $("#proxy_three_sel").val("${proxy.proxyThree.id}");
            }
        });
    }

    function checkForm() {
        var isEdit = ("${proxy.duty.id}" > 0);
        if(!isEdit){
            if ($("#duty_sel").children().length == 0) {
                alert("该组织结构没有职务，请先添加职务");
                return;
            }
            
            var duty = $("#duty_sel").val();
            if (duty == null || duty == 0) {
                alert("请选择职务");
                return;
            }
        }
        var employee = $("#employee_sel").val();
        if (employee == null || employee == 0) {
            alert("请选择任职人");
            return;
        }
        var isFirstSelected = isSelected("proxy_one_sel");
        var isSecondSelected = isSelected("proxy_two_sel");
        var isThirdSelected = isSelected("proxy_three_sel");
        //第一代理人未设置时，不可以先设置第二第三代理人
        if(!isFirstSelected){
            if(isSecondSelected || isThirdSelected){
                alert("请先设置第一代理人");
                return;
            }
        }else{
            if(!isSecondSelected && isThirdSelected){ //第二代理人未设置时，不可以设置第三代理人
                alert("请先设置第二代理人");
                return;
            }
        }
        
        var url = "${ctx}/duty_proxy/create.xhtml";
        $.ajax(url, {
            type: "POST",
            data: $("#post_form").serialize(),
            dataType:"html",
            cache:false,
            success:function(a) {
                return pageDownloaded(a, url)
            },
            complete:function() {
            }
        });
    }

    function isSelected(selectId){
        var obj_val = $("#"+selectId).val();
        return !(obj_val == null || obj_val == 0);
    }
</script>